package com.woniu.user.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.woniu.user.domain.Doctor;
import com.woniu.user.domain.Registration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.user.domain.Patient;
import com.woniu.user.service.IPatientService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 客户病历表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/patient")
public class PatientController extends BaseController {
    @Autowired
    private IPatientService patientService;

    /**
     * 查询客户病历表列表
     */
    @RequiresPermissions("user:patient:list")
    @GetMapping("/list")
    public TableDataInfo list(Patient patient)
    {
        startPage();
        List<Patient> list = patientService.selectPatientList(patient);
        return getDataTable(list);
    }

    /**
     * 导出客户病历表列表
     */
    @RequiresPermissions("user:patient:export")
    @Log(title = "客户病历表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patient patient)
    {
        List<Patient> list = patientService.selectPatientList(patient);
        ExcelUtil<Patient> util = new ExcelUtil<Patient>(Patient.class);
        util.exportExcel(response, list, "客户病历表数据");
    }


    @RequiresPermissions("user:patient:query")
    @GetMapping(value = "/test/{patientId}")
    public Patient getNameById(@PathVariable("patientId") Long patientId)
    {
        Patient patient = patientService.selectPatientById(patientId);
        return patient;
    }



    /**
     * 根据挂号流水信息新增客户病历表
     */
    @RequiresPermissions("user:patient:add")
    @Log(title = "客户病历表", businessType = BusinessType.INSERT)
    @PostMapping("/addPatient")
    public void addPatient(@RequestBody Registration registration)
    {
        Patient patient = new Patient();
        patient.setPatientName(registration.getPatientName());
        patient.setPatientIdcard(registration.getPatientIdcard());
        patient.setPatientAge(registration.getPatientAge());
        patient.setDoctorTel(registration.getDoctorTel());
        patient.setDickerSex(registration.getDickerSex());
        patient.setPatientVisittime(new Date());
        patientService.insertPatient(patient);
    }

    /**
     * 获取客户病历表详细信息
     */
//    @RequiresPermissions("user:patient:query")
//    @GetMapping(value = "/{patientId}")
//    public AjaxResult getInfo(@PathVariable("patientId") Long patientId)
//    {
//        return success(patientService.selectPatientByPatientId(patientId));
//    }

    @RequiresPermissions("user:patient:query")
    @GetMapping(value = "/{patientId}")
    public AjaxResult getpatient(@PathVariable("patientId") Long patientId)
    {
        return success(patientService.selectPatientByPatientId(patientId));
    }

    @RequiresPermissions("user:patient:query")
    @GetMapping("/getPatientList")
    public AjaxResult getPaientList()
    {
        return success(patientService.showAllPaient());
    }



    @GetMapping( "/patientyan/{patientName}")
    public Patient getPatientByNameYan(@PathVariable("patientName") String patientName) {
        Patient patientByNameYan = patientService.getPatientByNameYan(patientName);
        return patientByNameYan;
    }

    @RequiresPermissions("user:patient:query")
    @GetMapping(value = "/patientName/{patientId}")
    public String getPatientNameById(@PathVariable("patientId") Long patientId)
    {
        String patient = patientService.getPatientNameById(patientId);
        return patient;
    }

    @RequiresPermissions("user:patient:query")
    @GetMapping(value = "/patientIdByName/{patientName}")
    public Patient getPatientIdByName(@PathVariable("patientName") String patientName)
    {
        Patient patient = patientService.getPatientIdByName(patientName);
        return patient;
    }
    /**
     * 新增客户病历表
     */
    @RequiresPermissions("user:patient:add")
    @Log(title = "客户病历表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patient patient)
    {
        return toAjax(patientService.insertPatient(patient));
    }

    /**
     * 修改客户病历表
     */
    @RequiresPermissions("user:patient:edit")
    @Log(title = "客户病历表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patient patient)
    {
        return toAjax(patientService.updatePatient(patient));
    }

    /**
     * 删除客户病历表
     */
    @RequiresPermissions("user:patient:remove")
    @Log(title = "客户病历表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(patientService.deletePatientByPatientIds(patientIds));
    }

    @GetMapping("/pp/{id}")
    public Patient selectById(@PathVariable("id") long id){
        return patientService.selectById(id);
    }
    @GetMapping("/idCard/{idcard}")
    public Patient selByIdCard(@PathVariable("idcard") String patientIdcard){
        return patientService.selByIdCard(patientIdcard);
    }
}
